package com.manor.paradise.activity

import android.content.Intent
import android.net.Uri
import com.gyf.immersionbar.ktx.immersionBar
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.adapter.LybActiveIndexAdapter
import com.manor.paradise.bean.ReikindexBean
import com.manor.paradise.http.ElvesHttpUtil
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.lyb_activity_his_invite.*

class LybHisInviteActivity : AbsActivity() {
    private var pageNum = 1
    private var mId = ""
    private var mineBean: ReikindexBean.RecordsBean? = null
    private val mList = arrayListOf<ReikindexBean.RecordsBean>()
    private val mAdapter by lazy {
        LybActiveIndexAdapter(mList).apply {
            setOnItemClickListener { adapter, view, position ->
                ItInviteActivity.forward(this@LybHisInviteActivity, mList[position])
            }
            setOnItemChildClickListener { adapter, view, position ->
                when(view.id){
                    R.id.sPhoneIv ->{
                        val phoneNum = getItem(position).mobile
                        AndPermission.with(this@LybHisInviteActivity)
                                .runtime()
                                .permission(Permission.CALL_PHONE)
                                .onGranted {
                                    val intent = Intent(Intent.ACTION_DIAL)
                                    val data = Uri.parse("tel:$phoneNum")
                                    intent.data = data
                                    startActivity(intent)
                                }
                                .onDenied { data ->
                                }
                                .start()
                    }
                }
            }
        }
    }
    override fun getLayoutId() = R.layout.lyb_activity_his_invite

    override fun main() {
        super.main()
        immersionBar {
            statusBarColor(R.color.white)
            titleBar(mTitleBarLayout)
            statusBarDarkFont(true)
        }
        setTopTitle("TA的已邀请好友")
        mineBean = intent?.getSerializableExtra("mineBean") as ReikindexBean.RecordsBean?
        mId = mineBean?.id ?: ""
        if (StringUtil.isEmpty(mId)) {
            ToastUtil.show("信息错误")
            finish()
        }
        mHisInviteRv.adapter = mAdapter
        initMine()
        mPhoneIv.setOnClickListener {
            callPhone(mineBean?.mobile)
        }
        mHisInviteSrl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData(false)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData(true)
            }
        })
        setData(true)
    }

    private fun initMine() {
        mineBean?.let { item ->
            GlideUtil.loadImage(item.avatar, mAvatarIv, 50, 50)

            mNickNameTv.text = item.serialNo.front("淘米号 ")
            mTimeTv.text = item.createTime
            mPersonalActiveTv.text = item.anima
            mTeamActiveTv.text = item.teamLiveness
            mYiyangzhiYiyaoqingTv.text = "/".frontAndBack(item.lowerHireCount, item.lowerCount)
            when (item.star) {
                "-1" -> {
                    mStarTv.text = "无等级"
                    mStarIv.setBackgroundResource(R.drawable.star_no)
                }
                "0" -> {
                    mStarTv.text = "一星农场主"
                    mStarIv.setBackgroundResource(R.drawable.star1_lighten_b)
                }
                "1" -> {
                    mStarTv.text = "二星农场主"
                    mStarIv.setBackgroundResource(R.drawable.star2_lighten_b)
                }
                "2" -> {
                    mStarTv.text = "三星农场主"
                    mStarIv.setBackgroundResource(R.drawable.star3_lighten_b)
                }
                "3" -> {
                    mStarTv.text = "四星农场主"
                    mStarIv.setBackgroundResource(R.drawable.star4_lighten_b)
                }
            }
        }
    }

    fun setData(isRefresh: Boolean) {
        ElvesHttpUtil.getLowerInviteList(pageNum, mId, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    onError()
                    return
                }
                var reikindexBean: ReikindexBean = GsonUtil.gsonToBean<ReikindexBean>(info, ReikindexBean::class.java)
                KLog.e("json", reikindexBean)
                if (pageNum == 1) {
                    mList.clear()
                    mList.addAll(reikindexBean.records)
                    mAdapter.notifyDataSetChanged()
                    mHisInviteSrl.finishRefresh()
                } else {
                    mList.addAll(reikindexBean.records)
                    mAdapter.notifyDataSetChanged()
                    mHisInviteSrl.finishLoadMore()
                }
            }


            override fun onError() {
                super.onError()
                if (!isRefresh) {
                    pageNum--
                    mHisInviteSrl.finishLoadMore()
                }
            }
        })
    }
    companion object {
        fun launch(bean: ReikindexBean.RecordsBean) {
            val activity = ActivityManager.getInstance().currentActivity()
            val intent = Intent(activity, LybHisInviteActivity::class.java)
            intent.putExtra("mineBean", bean)
            activity.startActivity(intent)
        }
    }
}